/*******************************************************************************

ADL SCORM 2004 4th Edition Sample Run-Time Environment

The ADL SCORM 2004 4th Ed. Sample Run-Time Environment is licensed under
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States.

The Advanced Distributed Learning Initiative allows you to:
  *  Share - to copy, distribute and transmit the work.
  *  Remix - to adapt the work. 

Under the following conditions:
  *  Attribution. You must attribute the work in the manner specified by the author or
     licensor (but not in any way that suggests that they endorse you or your use
     of the work).
  *  Noncommercial. You may not use this work for commercial purposes. 
  *  Share Alike. If you alter, transform, or build upon this work, you may distribute
     the resulting work only under the same or similar license to this one. 

For any reuse or distribution, you must make clear to others the license terms of this work. 

Any of the above conditions can be waived if you get permission from the ADL Initiative. 
Nothing in this license impairs or restricts the author's moral rights.

*******************************************************************************/
package org.adl.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

// xerces imports

// adl imports

/**
 * <strong>Filename:</strong><br>
 * EnvironmentVariable.java<br><br>
 *
 * <strong>Description:</strong><br>
 * A <code>EnvironmentVariable</code> is used to access a specified
 * environment variable.
 
 * @author ADL Technical Team
 */
public final class EnvironmentVariable
{
   /**
    * Retrieves the value of the specified environment variable.
    *
    * @param iKey   Name of the environment variable.
    *
    * @return Value of the specified environment variable.
    */
   public static String getValue( String iKey )
   {
      String value = new String();

      try
      {
         Process p;
         String osName = System.getProperty("os.name");

         if ((osName.equalsIgnoreCase( "Windows 95" )) ||
             (osName.equalsIgnoreCase( "Windows 98" )) ||
             (osName.equalsIgnoreCase( "Windows Me" )))
         {
            p = Runtime.getRuntime().exec("command.com /c echo %" +
                                                  iKey + "%");
         }
         else
         {
            p = Runtime.getRuntime().exec("cmd.exe /c echo %" +
                                                  iKey + "%");
         }

         p.waitFor();
         BufferedReader br = new BufferedReader
              ( new InputStreamReader( p.getInputStream() ) );
         value = br.readLine();

         if ( value.startsWith("%") )
         {
            value = "";
         }

         br.close();
         p.destroy();
      }
      catch ( IOException ioe )
      {
         System.out.println("Could not read environment variable key " + iKey);
      }
      catch ( InterruptedException ie )
      {
         System.out.println("The process has been interrupted");
      }

      return value;
   }
}